package handler

import (
	"context"
	"encoding/json"
	"fmt"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/types/known/emptypb"
	"shop/goods-srv/global"
	"shop/goods-srv/model"
	categoryproto "shop/goods-srv/proto"
)

type CategoryService struct{}

func CategoryModelToResponse(category model.Category) categoryproto.CategoryInfoResponse {
	categoryResp := categoryproto.CategoryInfoResponse{
		Id:             category.ID,
		Name:           category.Name,
		ParentCategory: category.ParentCategoryId,
		Level:          category.Level,
		IsTab:          category.IsTab,
	}
	return categoryResp
}

//GetAllCategoryList(context.Context, *emptypb.Empty) (*CategoryListResponse, error)
//	GetSubCategory(context.Context, *CategoryListRequest) (*CategoryInfoResponse, error)
//	DeleteCategory(context.Context, *DeleteCategoryRequest) (*emptypb.Empty, error)
//	UpdateCategory(context.Context, *CategoryInfoRequest) (*emptypb.Empty, error)

//GetAllCategoryList(context.Context, *emptypb.Empty) (*CategoryListResponse, error)
//GetSubCategory(context.Context, *CategoryListRequest) (*CategoryInfoResponse, error)
//DeleteCategory(context.Context, *DeleteCategoryRequest) (*emptypb.Empty, error)
//UpdateCategory(context.Context, *CategoryInfoRequest) (*emptypb.Empty, error)
//CreateCategory(context.Context, *CategoryInfoRequest) (*CategoryInfoResponse, error)

func (s *CategoryService) CreateCategory(ctx context.Context, req *categoryproto.CategoryInfoRequest) (*categoryproto.CategoryInfoResponse, error) {
	var category model.Category
	category.Name = req.Name
	category.Level = req.Level
	if req.Level != 1 {
		category.ParentCategoryId = req.ParentCategory
	}
	category.IsTab = req.IsTab
	global.Db.Save(&category)

	return &categoryproto.CategoryInfoResponse{
		Id: int32(category.ID),
	}, nil
}

func (s *CategoryService) GetSubCategory(ctx context.Context, req *categoryproto.CategoryListRequest) (*categoryproto.SubCategoryListResponse, error) {
	var category model.Category
	categoryListResponse := categoryproto.SubCategoryListResponse{}

	if res := global.Db.First(&category, req.Id); res.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "商品分类不存在")
	}
	categoryListResponse.Info = &categoryproto.CategoryInfoResponse{
		Id:             category.ID,
		Name:           category.Name,
		ParentCategory: category.ParentCategoryId,
	}
	var subCategorys []model.Category
	var subCategoryResponse []*categoryproto.CategoryInfoResponse //

	global.Db.Where(&model.Category{ParentCategoryId: req.Id}).Find(&subCategorys)
	for _, subCategory := range subCategorys {
		subCategoryResponse = append(subCategoryResponse, &categoryproto.CategoryInfoResponse{
			Id:             subCategory.ID,
			Name:           subCategory.Name,
			Level:          subCategory.Level,
			IsTab:          subCategory.IsTab,
			ParentCategory: subCategory.ParentCategoryId,
		})
	}
	categoryListResponse.SubCategorys = subCategoryResponse
	return &categoryListResponse, nil
}
func (s *CategoryService) DeleteCategory(ctx context.Context, req *categoryproto.DeleteCategoryRequest) (*emptypb.Empty, error) {
	var categorys model.Category
	first := global.Db.First(&categorys, req.Id)
	if first.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "分类信息不存在")
	}
	global.Db.Delete(&categorys, categorys.ID)
	return &emptypb.Empty{}, nil
}

func (s *CategoryService) UpdateCategory(ctx context.Context, req *categoryproto.CategoryInfoRequest) (*emptypb.Empty, error) {
	var categorys model.Category
	first := global.Db.First(&categorys, req.Id)
	if first.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "分类信息不存在")
	}
	categorys.Name = req.Name
	global.Db.Save(&categorys)

	return &emptypb.Empty{}, nil
}

func (s *CategoryService) GetAllCategoryList(ctx context.Context, req *emptypb.Empty) (*categoryproto.CategoryListResponse, error) {
	var categorys []model.Category

	global.Db.Where(&model.Category{
		//Level: 1,
	}).Preload("SubCategory.SubCategory").Find(&categorys)

	b, _ := json.Marshal(&categorys)
	fmt.Println(string(b))
	return &categoryproto.CategoryListResponse{
		JsonData: string(b),
	}, nil
}
